/*
 * 文件名：RepairInfoController.java
 * 版权：Copyright by www.huawei.com
 * 描述：
 * 修改人：Administrator
 * 修改时间：2016年6月27日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.zcsy.manage.owner.controller.property;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.constants.OwnerConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.commons.util.Globals;
import com.zcsy.manage.model.base.CommonImg;
import com.zcsy.manage.model.base.CommonToken;
import com.zcsy.manage.model.property.RepairInfo;
import com.zcsy.manage.model.property.RepairReply;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.owner.service.common.IOCommonTokenService;
import com.zcsy.manage.owner.service.system.IOUserService;
import com.zcsy.manage.service.base.ICommonImgService;
import com.zcsy.manage.service.property.IRepairInfoService;
import com.zcsy.manage.service.property.IRepairReplyService;

/**
 * @Title: RepairInfoController.java
 * @Package： com.zcsy.manage.web.property
 * @Description: 报修信息管理类
 * @author yangfan
 * @Copyright: 2016 武汉恒数利通信息技术有限公司 All rights reserved.
 * @date 2016年6月27日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Controller
@RequestMapping(value = "repairInfo")
public class RepairInfoController {
    
    @Autowired
    private IRepairInfoService repairInfoService;
    
    @Autowired
    private IOCommonTokenService commomTokenService;
    
    @Autowired
    private IRepairReplyService repairReplyService;
    
    @Autowired
    private IOUserService ouserService;
    
    @Autowired
    private ICommonImgService commonImgService;
    
    /**
     * @Description 获取我的报修列表<br>
     * @param token
     * @return repairInfoList 报修列表
     * @author yangfan 373188993@qq.com
     * @date 2016年6月27日
     * @see JDK1.7.0_75
     */
    @RequestMapping(value = "maintenanceList")
    @ResponseBody
    public RequestJson maintenanceList(String token, Integer pNo) {

        RequestJson result = new RequestJson();

        try {
            // 指定默认为首页
            if (pNo == null || pNo < OwnerConstants.PAGE_NO) {
                pNo = OwnerConstants.PAGE_NO;
            }
            
            // 根据token查询tokenInfo
            CommonToken tokenInfo = commomTokenService.selectByToken(token);
            if (tokenInfo != null) {
                RepairInfo repairInfo = new RepairInfo();
                
                // 根据token查询登录名
                String loginname = tokenInfo.getAccount();
                // 根据登录名查询用户
                User user = ouserService.selectByLoginName(loginname);
                // 根据用户查询业主ID
                String userID = user.getId();
                repairInfo.setOwnerId(userID);
                
                // 此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
                PageHelper.startPage(pNo, OwnerConstants.PAGE_SIZE);
                
                List<RepairInfo> repairInfoList = repairInfoService.getRepairList(repairInfo);
                
                if (!repairInfoList.isEmpty()) {
                    PageInfo<RepairInfo> repairInfoListP = new PageInfo<RepairInfo>(repairInfoList);
                    result.setSuccess(true);
                    result.setResultCode(OwnerConstants.GET_REPAIRINFO_SUCCESS);
                    result.setMsg("获取成功！");
                    result.setObj(repairInfoListP);
                } else {
                    result.setSuccess(false);
                    result.setResultCode(OwnerConstants.GET_REPAIRINFO_FALSE);
                    result.setMsg("获取失败！");
                }
            } else {
                result.setSuccess(false);
                result.setResultCode(OwnerConstants.RESULT_EXCEPTION_FALSE);
                result.setMsg("未获取到token信息！");
            }
        } catch (Exception e) {
            result.setSuccess(false);
            result.setResultCode(OwnerConstants.RESULT_EXCEPTION_FALSE);
            result.setMsg("网络异常");
        }

        return result;
    }
    
    
    /**
     * @Description 获取我的报修详情<br>
     * @param token
     *        maintainId 报修id
     * @return repairInfoDetail 报修列表
     * @author yangfan 373188993@qq.com
     * @date 2016年6月27日
     * @see JDK1.7.0_75
     */
    @RequestMapping(value = "maintainDetail")
    @ResponseBody
    public RequestJson maintainDetail(String token , String maintainId ) {
        
        RequestJson result = new RequestJson();
        
        try {
            List<RepairReply> repairReply = repairReplyService.selectByRepairId(maintainId);
            RepairInfo repairInfo = repairInfoService.selectByPrimaryKey(maintainId);
            List<CommonImg> commonImg = commonImgService.selectImgsByObjIdAndObjType(maintainId,Constants.OWNER_REPAIRINFO);
            if(commonImg.size() > 0){
                //遍历，将图片url加上前缀
                for (CommonImg item : commonImg) {
                    item.setUrl(Globals.getImageUrl()+item.getUrl());
                } 
            }
            if (repairInfo != null )  {
                Map<String , Object> repairDetail = new HashMap<String , Object>();
                
                repairDetail.put("photoUrl", commonImg);
                repairDetail.put("repairRelpy", repairReply);
                repairDetail.put("repairInfo", repairInfo);
                result.setSuccess(true);
                result.setResultCode(OwnerConstants.GET_REPAIRDETAIL_SUCCESS);
                result.setMsg("获取成功！");
                result.setObj(repairDetail);
            } else {
                result.setSuccess(false);
                result.setResultCode(OwnerConstants.GET_REPAIRDETAIL_FALSE);
                result.setMsg("获取失败！");
            }
        } catch (Exception e) {
            result.setSuccess(false);
            result.setResultCode(OwnerConstants.RESULT_EXCEPTION_FALSE);
            result.setMsg("网络异常");
        }

        return result;
    }
}
